package com.hzdl.hotel.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.hotel.bean.Customer;
import com.hzdl.hotel.service.CustomerService;
import com.hzdl.hotel.util.AJAXResult;
import com.hzdl.hotel.util.CustomExcelUtil;
import com.hzdl.hotel.vo.CustomerVo;
import com.hzdl.hotel.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService cs;
    @RequestMapping("/customerInit")
    public String customerInit(){
        return "administer/guestroom/customerManage";
    }

    @RequestMapping("/customerShow")
    @ResponseBody
    public AJAXResult customerShow(CustomerVo customerVo){
        if (customerVo.getPage() == null) {
            customerVo.setPage(1);
        }
        if (customerVo.getLimit() == null) {
            customerVo.setLimit(5);
        }
        PageInfo pageInfo = cs.findUserByWhere(customerVo);
        return new AJAXResult(0, "", pageInfo.getList(), pageInfo.getTotal());
    }


    @RequestMapping("/customerExport")
    @ResponseBody
    public ResponseEntity<Object> customerExport (CustomerVo customerVo){
        List<Customer> cList =cs.findUserByWhere1(customerVo);
        ByteArrayOutputStream stream = CustomExcelUtil.create2(cList);
        String fileName = null;
        //导出文件名称
        try {
            fileName = URLEncoder.encode("客户信息.xls","UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //设置下载是的头部响应对象
        HttpHeaders httpHeader = new HttpHeaders();
        httpHeader.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        httpHeader.setContentDispositionFormData("attachment",fileName);

        return new ResponseEntity<Object>(stream.toByteArray(),httpHeader, HttpStatus.CREATED);
    }

}
